package com.jbit.hxy.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.feign.MenuFeignClient;
import com.jbit.hxy.mapper.WxShoppingCartMapper;
import com.jbit.hxy.model.po.WxDish;
import com.jbit.hxy.model.po.WxDishFlavor;
import com.jbit.hxy.model.po.WxSetmealDish;
import com.jbit.hxy.model.po.WxShoppingCart;
import com.jbit.hxy.model.vo.DishDetailVO;
import com.jbit.hxy.model.vo.WxDishFlavorVO;
import com.jbit.hxy.model.vo.WxShoppingCartSetmealVO;
import com.jbit.hxy.model.vo.WxShoppingCartVO;
import com.jbit.hxy.service.order.WxShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@RequiredArgsConstructor
@Tag(name = "购物车")
public class WxShoppingCartController {

    private final WxShoppingCartService wxShoppingCartService;
    private final WxShoppingCartMapper wxShoppingCartMapper;
    @Resource
    private MenuFeignClient menuFeignClient;
    @GetMapping("/getShoppingCartByUserId")
    @Operation(summary = "根据用户id查询购物车")
    public Result getShoppingCartByUserId(@RequestParam("userId") Integer userId){
        return wxShoppingCartService.getShoppingCartByUserId(userId);

    }
    @PostMapping("/addShoppingCart")
    @Operation(summary = "添加购物车")
    public Result addShoppingCart(@RequestBody WxShoppingCart wxShoppingCart){
        return wxShoppingCartService.addShoppingCart(wxShoppingCart);
    }
    @PostMapping("/updateShoppingCart")
    @Operation(summary = "修改购物车")
    public Result updateShoppingCart(@RequestBody List<WxShoppingCart> wxShoppingCartList){
        return wxShoppingCartService.updateShoppingCart(wxShoppingCartList);
    }
    @GetMapping("/getShoppingCartClear")
    @Operation(summary = "清空购物车")
    public Result getShoppingCartClear(@RequestParam("userId") Integer userId){
        return wxShoppingCartService.getShoppingCartClear(userId);
    }
    @GetMapping("/getShoppingCartDetailByUserId")
    @Operation(summary = "根据用户id查询购物车中菜品详情")
    public Result getShoppingCartDishDetailByUserId(@RequestParam("userId") Integer userId){
        List<WxShoppingCart> shoppingCartByUserId = wxShoppingCartMapper.getShoppingCartByUserId(userId);
        ArrayList<WxShoppingCartVO> wxShoppingCartVOS = new ArrayList<>();
        if (shoppingCartByUserId == null) {
            return Result.fail("购物车为空");
        }
        for (WxShoppingCart wxShoppingCart : shoppingCartByUserId) {
            if (wxShoppingCart.getDishId() != null) {
                WxShoppingCartVO wxShoppingCartVO = new WxShoppingCartVO();
                BeanUtils.copyProperties(wxShoppingCart, wxShoppingCartVO);

                List<Object> list = Collections.nCopies(wxShoppingCart.getNum(), null);
                wxShoppingCartVO.setSelectValue(list);
                Result<List<WxDishFlavorVO>> flavorByDishId = menuFeignClient.getFlavorByDishId(wxShoppingCart.getDishId());
                Result dishNameById = menuFeignClient.getDishNameById(wxShoppingCart.getDishId());
                String name = dishNameById.getMsg();
                wxShoppingCartVO.setName(name);
                List<WxDishFlavorVO> data = flavorByDishId.getData();
                wxShoppingCartVO.setWxDishFlavors(data);
                wxShoppingCartVOS.add(wxShoppingCartVO);
            }

        }
        return Result.success(wxShoppingCartVOS);

    }
    @GetMapping("/getShoppingCartSetmealDetailByUserId")
    @Operation(summary = "根据用户id查询购物车中套餐详情")
    public  Result getShoppingCartSetmealDetailByUserId(@RequestParam("userId") Integer userId) {
        List<WxShoppingCart> shoppingCartByUserId = wxShoppingCartMapper.getShoppingCartByUserId(userId);
        ArrayList<WxShoppingCartSetmealVO> wxShoppingCartSetmealVOS = new ArrayList<>();
        if (shoppingCartByUserId == null) {
            return Result.fail("购物车为空");
        }
        for (WxShoppingCart wxShoppingCart : shoppingCartByUserId) {
            if (wxShoppingCart.getDishId() == null) {
                WxShoppingCartSetmealVO wxShoppingCartSetmealVO = new WxShoppingCartSetmealVO<>();
                BeanUtils.copyProperties(wxShoppingCart, wxShoppingCartSetmealVO);
                Result dishCountBySetmealId = menuFeignClient.getDishCountBySetmealId(wxShoppingCart.getSetmealId());
                Integer id = wxShoppingCart.getSetmealId();
                Result setmealNameById = menuFeignClient.getSetmealNameById(id);
                String name = setmealNameById.getMsg();
                wxShoppingCartSetmealVO.setName(name);
                Result<List<WxSetmealDish>> setmealDishBySetmealId = menuFeignClient.getSetmealDishBySetmealId(id);
                ArrayList<DishDetailVO> dishDetailVOS = new ArrayList<>();
                for (WxSetmealDish setmealDish : setmealDishBySetmealId.getData()){
                    Result<WxDish> wxDishResult = menuFeignClient.getDishById(setmealDish.getDishId());
                    WxDish wxDish = wxDishResult.getData();
                    DishDetailVO dishDetailVO = new DishDetailVO();
                    BeanUtils.copyProperties(wxDish,dishDetailVO);
                    dishDetailVO.setNum(setmealDish.getCopies());
                    Result<List<Integer>> flavorIdByDishId = menuFeignClient.getFlavorIdByDishId(wxDish.getId());
                    List<Integer> data = flavorIdByDishId.getData();
                    ArrayList<WxDishFlavorVO> wxDishFlavorVOS = new ArrayList<>();
                    for (Integer flavorId : data) {
                        Result<WxDishFlavor> wxDishFlavorResult = menuFeignClient.selectFlavorById(flavorId);
                        WxDishFlavor wxDishFlavor = wxDishFlavorResult.getData();
                        String values = wxDishFlavor.getValues();
                        Gson gson = new Gson();
                        Type listType = new TypeToken<List<String>>() {}.getType();
                        List<String> sugarLevels = gson.fromJson(values, listType);
                        WxDishFlavorVO wxDishFlavorVO = new WxDishFlavorVO();
                        BeanUtils.copyProperties(wxDishFlavor,wxDishFlavorVO);
                        wxDishFlavorVO.setValues(sugarLevels);

                        wxDishFlavorVOS.add(wxDishFlavorVO);


                    }
                    dishDetailVO.setWxDishFlavors(wxDishFlavorVOS);
                    dishDetailVOS.add(dishDetailVO);
                }

                wxShoppingCartSetmealVO.setDishDetailVOList(dishDetailVOS);
                wxShoppingCartSetmealVO.setSelectValue(createThreeDimensionalList(wxShoppingCartSetmealVO));
                wxShoppingCartSetmealVOS.add(wxShoppingCartSetmealVO);

            }

        }
        return Result.success(wxShoppingCartSetmealVOS);

    }
    public static ArrayList<ArrayList<ArrayList<Object>>> createThreeDimensionalList(
            WxShoppingCartSetmealVO wxShoppingCartSetmealVO) {
        ArrayList<ArrayList<ArrayList<Object>>> threeDList = new ArrayList<>();

        // 创建第一层的ArrayList
        for (int i = 0; i < wxShoppingCartSetmealVO.getNum(); i++) {
            ArrayList<ArrayList<Object>> secondLevelList = new ArrayList<>();
            List<DishDetailVO> dishDetailVOList = wxShoppingCartSetmealVO.getDishDetailVOList();
            // 创建第二层的ArrayList
            for (int j = 0; j < dishDetailVOList.size(); j++) {
                ArrayList<Object> thirdLevelList = new ArrayList<>();
                DishDetailVO dishDetailVO = dishDetailVOList.get(j);

                // 创建第三层的ArrayList并填充null值
                for (int k = 0; k < dishDetailVO.getNum(); k++) {
                    thirdLevelList.add(null);
                }

                secondLevelList.add(thirdLevelList);
            }

            // 把第二层的ArrayList添加到第一层的ArrayList
            threeDList.add(secondLevelList);
        }

        return threeDList;
    }
}
